package com.zhongwang.business.module.quoteManage.quoteManageController;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.*;
import com.zhongwang.business.module.basic.model.DictEnumModel;
import com.zhongwang.business.module.quoteManage.quoteManageDao.QuotationApproveDao;
import com.zhongwang.business.module.quoteManage.quoteManageModel.FileManageModel;
import com.zhongwang.business.module.quoteManage.quoteManageModel.QuotationMainModel;
import com.zhongwang.business.module.quoteManage.quoteManageDao.QuotationMainDao;
import com.zhongwang.business.module.quoteManage.quoteManageDao.QuotationQueryDao;
import com.zhongwang.business.gen.mapper.SellerInfoMapper;
import com.zhongwang.business.module.quoteManage.quoteManageModel.QuotationSublistModel;
import com.zhongwang.business.module.quoteManage.quoteManageService.QuoteTaskService;
import com.zhongwang.business.module.system.log.LogService;
import com.zhongwang.business.module.system.user.UserService;
import com.zhongwang.business.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 报价任务管理
 * @author zcy
 * @time: 2020-08-20
 */

@Controller
@RequestMapping("/quoteManage")
public class QuoteTaskManageController extends BaseController {
    @Autowired
    private QuoteTaskService quoteTaskService;
    @Autowired
    private UserService userService;
    @Autowired
    private QuotationMainDao quotationMainDao;
    @Autowired
    private LogService logService;
    @Autowired
    private QuotationApproveDao quotationApproveDao;
    @Autowired
    private QuotationQueryDao quotationQueryDao;
    @Autowired
    private SellerInfoMapper sellerInfoMapper;

    /**
     * 待报价任务列表页面
     * @param
     * @return 页面路径
     */
//    @GetMapping("/quoteTaskList/page")
//    @RequiresPermissions({"quo:list:view"})
//    public String page(Model model) {
//        return "quoteManage/quoteTaskList";
//    }

    /**
     * 待报价任务列表
     * @param query 查询条件
     * @return 列表数据
     */
//    @ResponseBody
//    @PostMapping("/quoteTaskList/list")
////    @RequiresPermissions({"quo:list:view"})
//    public JsonResult quoteTasklist(QuotationMainModel query){
//        startPage();
//        List<QuotationMainModel> list = quoteTaskService.findQuoteTaskList(query);
//        return jsonResult(list);
//    }

    /**
     * 选择待报价任务的列表
     * @return 列表数据
     */
    @ResponseBody
    @PostMapping("/createQuotation/qttTaskList")
    //    @RequiresPermissions({"quo:list:view"})
    public JsonResult qttTasklist(QuotationMainModel query){
        startPage();
        List<QuotationMainModel> list = quoteTaskService.findQttTaskList(query);
        return jsonResult(list);
    }

    /**
     * 从待报价任务列表跳转创建报价单页面
     * @param quoteTaskId,model 报价任务id
     * @return 页面路径
     */
//    @GetMapping("/createQuotation/page/{quoteTaskId}")
//    @RequiresPermissions({"quo:list:quotation"})
//    public String createTaskIdPage(@PathVariable("quoteTaskId") Integer quoteTaskId, Model model) {
//        model.addAttribute("quoteTaskInfo",quoteTaskService.findQuoteTaskInfo(quoteTaskId));
//        model.addAttribute("consumers",quoteTaskService.findQttConsInfoList());
//        User user = ShiroUtils.getUser();
//        model.addAttribute("userName", userService.findByAccount(user.getAccount()));
//        model.addAttribute("payItems",quotationMainDao.findPayItem());
//        return createPath();
//    }

    /**
     * 创建报价单页面
     * @return 页面路径
     */
    @GetMapping("/createQuotation/page")
    @RequiresPermissions({"quo:create:view"})
    public String createPage(Model model) {
        model.addAttribute("consumers",quoteTaskService.findQttConsInfoList());
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", userService.findByAccount(user.getAccount()));
        model.addAttribute("payItems", quotationMainDao.findPayItem());
        model.addAttribute("sellers", quotationMainDao.selectSellInfoList());
        return  "quoteManage/createQuotation";
    }


//
//    /**
//     * 查找选择的需方公司所有联系人列表
//     * @param consName 需方公司名
//     * @return 联系人列表
//     */
//    @PostMapping("/createQuotation/queryConsumerInfo")
//    @ResponseBody
//    public List<ConsumerInfoMainModel> findConsumerList (String consName){
//        return quoteTaskService.findConsumerList(consName) ;
//    }

    /**
     * 查找款项列表
     * @param
     * @return
     */
    @PostMapping("/createQuotation/payItem")
    @ResponseBody
    public List<DictEnumModel> findPayItem(){
        return quotationMainDao.findPayItem();
    }


    /**
     * 查找联系人信息
     * @param consInfoId 需方信息表id
     * @return 选择的需方联系人信息
     */
    @PostMapping("/createQuotation/queryConsAttnInfo")
    @ResponseBody
    public QuotationMainModel queryConsAttnInfo (Integer consInfoId){
        return quoteTaskService.findConsumer(consInfoId);
    }


    /**
     * 查找供方联系人信息
     * @param id 供方信息表id
     * @return 选择的供方联系人信息
     */
    @PostMapping("/createQuotation/querySupplInfo")
    @ResponseBody
    public SellerInfo querySellInfo (Integer id){
        return sellerInfoMapper.selectByPrimaryKey(id);
    }
    /**
     * 保存报价单主单和明细
     * @param quotationMainModel 报价单主单信息
     * @return 结果
     */
    @PostMapping("/createQuotation/editSave")
    @ResponseBody
    @RequiresPermissions( {"quo:await:edit"})
    public JsonResult saveQuotationMain(QuotationMainModel quotationMainModel, String[] proName, String[] proNames, String[] proType, Integer[] proQty,
                                        String[] proUnit, Double[] unitPrice, Double[] totalPrice, Double[] taxRate, Double[] taxAmount,
                                        Double[] unitPriceTaxIncluded, Double[] totalPriceIncludeTax, String[] remark, String[] taskIds, Integer[] payItem,
                                        String[] payCondition, Integer[] payPercent, Double[] payAmount, Integer[] payMethod){
        if(quotationMainModel.getQuoteId() != null){
            return quoteTaskService.quotationUpdateSave(
                    quotationMainModel, proName, proNames, proType,proQty, proUnit,
                    unitPrice, totalPrice, taxRate, taxAmount,
                    unitPriceTaxIncluded, totalPriceIncludeTax, remark, taskIds, payItem,
                    payCondition, payPercent, payAmount, payMethod
            );
        }else {
            return quoteTaskService.quotationCreateSave(quotationMainModel, proName, proNames, proType, proQty, proUnit,
                    unitPrice, totalPrice, taxRate, taxAmount,
                    unitPriceTaxIncluded, totalPriceIncludeTax, remark, taskIds, payItem,
                    payCondition, payPercent, payAmount, payMethod);
        }
    }

    /**
     * 报价明细项，从报价任务列表选择
     * @param quotationSublistModel 报价列表选择的报价任务id
     * @return 列表数据
     */
    @ResponseBody
    @PostMapping("/createQuotation/list")
    public JsonResult list(QuotationSublistModel quotationSublistModel){
        startPage();
        List<QuotationSublistModel> list = new ArrayList<>();
        if(quotationSublistModel.getQuoteId() != null){
            list = quoteTaskService.findQttList(quotationSublistModel.getQuoteId());
        }
        return jsonResult(list);
    }

    /**
     * 根据id查找报价明细
     * @param qtt 报价明细
     * @return json 结果
     */
    @ResponseBody
    @PostMapping("/createQuotation/findQttById")
    public QuotationSublist findQtt(QuotationSublist qtt){
        return findQttById(qtt);
    }

    /**
     * 根据报价明细id，查找报明细信息
     * @param qtt 报价明细
     * @return 报价明细信息
     */
    public QuotationSublist findQttById(QuotationSublist qtt) {
        QuotationSublist quotationSublist = quoteTaskService.getQttById(qtt.getQttId());
        return quotationSublist;
    }

    /**
     * 报价文件列表
     * @param quotationMainModel 根据报价单id查询文件列表
     * @return 列表数据
     */
    @ResponseBody
    @PostMapping("/createQuotation/fileList")
    public JsonResult fileList(QuotationMainModel quotationMainModel){
        startPage();
        List<FileManageModel> list = new ArrayList<>();
        if(quotationMainModel.getQuoteId() != null){
            list = quoteTaskService.findQttFileList(quotationMainModel.getQuoteId());
        }
        return jsonResult(list);
    }

    /**
     * 上传和修改文件
     * @param fileManageModel doc
     * @param file 文件
     * @return json
     */
    @ResponseBody
    @PostMapping("/file/upload")
    @RequiresPermissions({"quo:await:upload"})
    public JsonResult upload(FileManageModel fileManageModel, @RequestParam("file") MultipartFile[] file) {
        return quoteTaskService.upload(fileManageModel, file);
    }

    /**
     * 删除文件
     * @param doc doc 报价文件信息
     * @return json 结果
     */
    @ResponseBody
    @PostMapping("/file/del")
    @RequiresPermissions({"quo:await:delete"})
    public JsonResult delFile(FileManage doc) {
        findFileById(doc);
        logService.saveLog("删除文件", doc.getFileName());
        doc.setFileStatus(0);
        return jsonResult(quoteTaskService.updateDoc(doc));
    }

    /**
     * 根据id查找文件
     * @param doc doc 报价文件信息
     * @return json 结果
     */
    @ResponseBody
    @PostMapping("/file/findFileById")
    public FileManage findFile(FileManage doc){
        return findFileById(doc);
    }

    /**
     * 根据报价文件id，查找报价文件信息
     * @param doc 报价文件信息
     * @return 报价文件信息
     */
    public FileManage findFileById(FileManage doc) {
        FileManage fileManage = quoteTaskService.getFileById(doc.getFileId());
        return fileManage;
    }


//    /**
//     * 根据报价任务id查看报价单页面
//     * @param quoteTaskId,model
//     * @return 页面路径
//     */
//    @GetMapping("/viewQuotation/quoteTaskId/{quoteTaskId}")
//    @RequiresAuthentication
//    public String page(@PathVariable("quoteTaskId") Integer quoteTaskId, Model model) {
//        QuotationMainModel quotationMain = quotationMainDao.selectQuotationInfo(quoteTaskId);
//        model.addAttribute("quotationMain",quotationMain);
//        model.addAttribute("qtts", quotationMainDao.selectQttList(quotationMain.getQuoteId()));
//        model.addAttribute("payItems",quotationMainDao.selectQttItemList(quotationMain.getQuoteId()));
//        return returnPath ();
//    }

    /**
     * 根据报价单id查看报价单页面
     * @param quoteId,model
     * @return 页面路径
     */
    @GetMapping("/viewQuotation/quoteId/{quoteId}")
    @RequiresAuthentication
        @RequiresPermissions({"quo:await:view"})
    public String viewPage(@PathVariable("quoteId") Integer quoteId, Model model) {
        QuotationMainModel quotationMain = quotationMainDao.selectQttInfo(quoteId);
        model.addAttribute("quotationMain",quotationMain);
        model.addAttribute("qtts", quotationMainDao.selectQttList(quoteId));
        User user = ShiroUtils.getUser();
        model.addAttribute("sysUserModel", quotationApproveDao.findNowNodeByAccount(user.getAccount()));
        model.addAttribute("payItems",quotationMainDao.selectQttItemList(quoteId));
        return returnPath ();
    }

    /**
     * 根据报价单id查看打印报价单页面
     * @param quoteId,model
     * @return 页面路径
     */
    @GetMapping("/printQuotation/quoteId/{quoteId}")
    @RequiresAuthentication
    public String printPage(@PathVariable("quoteId") Integer quoteId, Model model) {
        QuotationMainModel quotationMain = quotationMainDao.selectQttInfo(quoteId);
        model.addAttribute("quotationMain",quotationMain);
        model.addAttribute("qtts", quotationMainDao.selectQttList(quoteId));
        model.addAttribute("payItems",quotationMainDao.selectQttItemList(quoteId));
        return "quoteManage/printQuotation";
    }

    /**
     * 返回查看报价任务页面
     * @return 返回查看报价任务路径
     */
    public String returnPath(){
        return "quoteManage/viewQuotation";
    }

    /**
     * 报价单列表页面
     * @param
     * @return 页面路径
     */
    @GetMapping("/quotationManage/page")
    @RequiresPermissions({"quo:com:view"})
    public String qttPage(Model model) {
        User user = ShiroUtils.getUser();
        model.addAttribute("sysUserModel", quotationQueryDao.findUserInfoByAccount(user.getAccount()));
        model.addAttribute("sellers",quotationMainDao.selectSellInfoList());
        return "quoteManage/quotationManage";
    }

    /**
     * 报价单列表
     * @param query 查询条件
     * @return 列表数据
     */
    @ResponseBody
    @PostMapping("/quotationManage/list")
    public JsonResult quotelist(QuotationMainModel query){
        startPage();
        List<QuotationMainModel> list = quoteTaskService.findQuoteList(query);
        return jsonResult(list);
    }

    /**
     * 根据报价单id编辑报价单页面
     * @param quoteId,model
     * @return 页面路径
     */
    @GetMapping("/editQuotation/{quoteId}")
    @RequiresAuthentication
    @RequiresPermissions({"quo:com:edit"})
    public String editPage(@PathVariable("quoteId") Integer quoteId, Model model) {
        model.addAttribute("consumers",quoteTaskService.findQttConsInfoList());
        model.addAttribute("quotationMain",quotationMainDao.selectQttInfo(quoteId));
        model.addAttribute("quotation",quotationMainDao.selectQttList(quoteId));
        model.addAttribute("payItemList",quotationMainDao.selectQttItemList(quoteId));
        model.addAttribute("payItems",quotationMainDao.findPayItem());
        List<QuotationMainModel> appList= quotationApproveDao.selectAppList(quoteId);
        model.addAttribute("appList",appList);
        model.addAttribute("sellers",quotationMainDao.selectSellInfoList());
        return "quoteManage/editQuotation";
    }

    /**
     * 删除报价
     * @param quotationMain
     * @return
     */
    @ResponseBody
    @PostMapping("/quotationManage/del")
    @RequiresPermissions({"quo:com:edit"})
    public JsonResult delQuote(QuotationMain quotationMain) {
        return quoteTaskService.delQuote(quotationMain);
    }

    /**
     * 提交报价
     * @param quotationMain
     * @return
     */
    @ResponseBody
    @PostMapping("/quotationManage/submit")
    @RequiresPermissions({"quo:com:commit"})
    public JsonResult submitQuote(QuotationMain quotationMain) {
        return quoteTaskService.submitQuote(quotationMain);
    }


    /**
     * 查找创建人
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/updateCreateAccount/selectSysUser")
    public JsonResult selectCreatersList() {
        List<SysUser> userList = quotationMainDao.selectCreatersList();
        return success().setData(userList);
    }
    /**
     * 修改报价创建人
     * @param quotationMain
     * @return
     */
    @ResponseBody
    @PostMapping("/updateCreateAccount")
    public JsonResult updateCreateAccount(QuotationMainModel quotationMain) {
        return  quoteTaskService.updateCreateAccount(quotationMain);
    }

    /**
     * 添加待报价任务的列表
     * @param query
     * @return
     */
//    @ResponseBody
//    @PostMapping("/quoteList/list")
////    @RequiresPermissions({"halwar:halallinwar:view"})
//    public JsonResult quoteAddlist(QuotationMainModel query){
//        startPage();
//        List<QuotationMainModel> list = quoteTaskService.findQuoteAddList(query);
//        return jsonResult(list);
//    }

    /**
     * 根据输入的字符串查找项目
     * @param q 输入的字符串
     * @return 项目编号列表
     */
//    @GetMapping("/add/project/list")
//    @ResponseBody
//    public List<TaskModelQ> proList(String q) {
//        return quoteTaskService.findProjectByCode(q);
//    }
}
